#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

void findAllPaths(vector<vector<int>>& graph, int curr, int dest, vector<int>& path, vector<vector<int>>& paths) {
    path.push_back(curr);

    if (curr == dest) {
        paths.push_back(path);
    } else {
        for (int i = 0; i < graph.size(); i++) {
            if (graph[curr][i] != 0) {
                findAllPaths(graph, i, dest, path, paths);
            }
        }
    }

    path.pop_back();
}

vector<vector<int>> findPaths(int n, vector<vector<int>>& graph, int src, int dest) {
    vector<vector<int>> paths;
    vector<int> path;

    findAllPaths(graph, src, dest, path, paths);

    return paths;
}

int main() {
    ifstream inputFile("input.txt");  // 打开输入文件
    ofstream outputFile("output.txt");  // 打开输出文件

    int n;
    inputFile >> n;

    vector<vector<int>> graph(n, vector<int>(n, 0));
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            inputFile >> graph[i][j];  // 从输入文件读取图的数据
        }
    }

    int src, dest;
    inputFile >> src >> dest;  // 从输入文件读取源节点和目标节点

    vector<vector<int>> paths = findPaths(n, graph, src, dest);  // 查找所有路径

    cout << "两点之间的所有路径是：" << endl;
    for (const auto& path : paths) {  // 遍历所有路径
        for (int i = 0; i < path.size(); i++) {
            outputFile << path[i];  // 将路径写入输出文件
            cout << path[i];
            if (i != path.size() - 1) {
                outputFile << " -> ";
                cout << " -> ";
            }
        }
        outputFile << endl;
        cout << endl;
    }
    cout << "已写入文件output.txt" << endl;

    inputFile.close();  // 关闭输入文件
    outputFile.close();  // 关闭输出文件

    return 0;
}